package com.jscm.openprocess;

import com.jscm.openmodule.OpenModuleBtn;
import com.jscm.showdata.SelectDataView;
import com.jscm.showdata.TagMomeory;
import com.scm.all.export.ByteUtils;
import com.scm.all.export.ProcessAndThreadUtils;
import com.scm.all.export.SystemUtils;
import com.scm.all.export.WindowOperationUtils;
import com.scm.all.pfunc.MemoryCallBack;
import com.scm.x64.export.MemoryOperationUtilsX64;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.scene.control.ListView;
import javafx.scene.input.MouseEvent;

/**
 * 列表组件
 */
public class OpenProcessListView {
    public static ListView<String> listView = new ListView();
    public static int processHandler = 0;
    public static int processId = 0;
    public static ObservableList<String> observableList = FXCollections.observableArrayList();
    static {
        init();
    }
    public static void init() {
        observableList.clear();
        listView.getItems().clear();
        processHandler = 0;
        processId = 0;
        listView.setPrefWidth(282);
        listView.setPrefHeight(460);
        listView.setStyle("-fx-background-color: #f5f4e6;");
        for(com.scm.all.struct.TagProcessenTry32 tagProcessenTry32:ProcessAndThreadUtils.processEnumInfo()){
            observableList.add(tagProcessenTry32.szExeFile);
        }
        listView.setItems(observableList);
        listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
                if(newValue !=null){
                    processId = ProcessAndThreadUtils.processNameGetPid(newValue);

                }
            }
        });
        listView.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                if(event.getClickCount()==2 && event.getButton().name().equals("PRIMARY")) {
                    processHandler = ProcessAndThreadUtils.openProcess(processId);
                    if(processHandler>0){
                        //进程打开成功以后解除模块禁止
                        OpenModuleBtn.button.setDisable(false);
                        System.err.println("进程打开成功");
                        OpenProcessWindow.stage.hide();
                    }else {
                        SystemUtils.messageBoxExW(WindowOperationUtils.windowGetHwnd(),"进程打开失败","温馨提示",0);
                        System.err.println("进程打开失败");
                    }
                }
            }
        });
    }
}
